package boot3.service.impl;

import boot3.mapper.TeacherMapper;
import boot3.mapper.UserMapper;
import boot3.pojo.Teacher;
import boot3.pojo.User;
import boot3.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TeacherServiceImpl implements TeacherService {
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    public List<Teacher> getTeacher(String teacher_name) {
        return teacherMapper.getTeacher(teacher_name);
    }

    @Override
    public List<Teacher> getTeachersByCollege(String college) {
        return teacherMapper.getTeachersByCollege(college);
    }

    @Override
    public Teacher getTeacherById(String teacher_id) {
        return teacherMapper.getTeacherById(teacher_id);
    }

    @Override
    public Boolean updateTeacher(String teacher_id, String item, String value) {
        return teacherMapper.updateTeacher(teacher_id, item, value);
    }

    @Override
    public Boolean deleteTeacher(String teacher_id) {
        return userMapper.delUser(teacher_id) && teacherMapper.deleteTeacher(teacher_id);
    }

    @Override
    public Boolean addTeacher(Teacher teacher) {
        //获取当前所有的用户信息
        List<User> allUsers= userMapper.findAll();
        String lastID=allUsers.get(allUsers.size()-1).getUserID();
        String newUserID= String.valueOf(Long.parseLong(lastID)+1);
        teacher.setTeacher_id(allUsers.isEmpty()?"202011070100":newUserID);
        User user=new User();
        user.setUserID(teacher.getTeacher_id());
        user.setUserName(teacher.getTeacher_name());
        user.setType("tea");
        user.setPassword("123123");
        return  userMapper.addUser(user) && teacherMapper.insertTeacher(teacher);
    }
}
